package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.QueryObject.RegionQuery;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.respository.DestinationRespository;
import cn.wolfcode.luowowo.respository.RegionRespository;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时不管事务
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRespository regionrRespository;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private DestinationRespository destinationRespository;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Region get(String id) {
        Optional<Region> optional = regionrRespository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<Region> list() {
        return regionrRespository.findAll();
    }

    @Override
    //Page对象里面包括 : 总页数 每页显示条数
    //使用mongoTemplate 只能返回Page 需要把list封装成 page对象再返回
    public Page<Region> query(RegionQuery qo) {
        /*//1.查询总条数 total
        Query query = new Query();
        long total = mongoTemplate.count(query, Region.class);
        if (total == 0) {
            return Page.empty();//空的分页对象
        }
        //2.每页显示的结果集 list
        //设置当前的显示条数 和 当前页
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "sequence");
        query.with(pageable);
        List<Region> regions = mongoTemplate.find(query, Region.class);

        //3.封装成Page对象
        return new PageImpl<Region>(regions, pageable, total);*/

        //使用工具类
        Query query = new Query();
        return DBHelper.query(mongoTemplate, Region.class, query, qo.getPageable());
    }

    @Override
    public void saveOrUpdate(Region region) {
        if (!StringUtils.hasLength(region.getId())) {
            //添加
            region.setId(null);
            regionrRespository.save(region);
        } else {
            //更新 方案一: 1.查询 2.替代 3.save

            //方案二:
            //条件
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(region.getId()));
            //更新的列
        /*    Update update = new Update();
            update.set("name", region.getName());
            update.set("sn", region.getSn());
            update.set("refIds", region.getRefIds());
            update.set("ishot", region.getIshot());
            update.set("sequence", region.getSequence());
            update.set("info", region.getInfo());
            mongoTemplate.updateMulti(query, update, Region.class);*/
            //使用工具类 可替代上面
            DBHelper.update(mongoTemplate, query, region,
                    "name", "sn", "refIds", "ishot", "sequence", "info");
        }
    }

    @Override
    public void changeHotValue(String id, int hot) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("ishot", hot);
        mongoTemplate.updateMulti(query, update, Region.class);
    }

    @Override
    public void delete(String id) {
        regionrRespository.deleteById(id);
    }


    @Override
    public List<Region> queryHotRegion() {
        return regionrRespository.findByIshotOrderBySequenceAsc(Region.STATE_HOT);
    }


}
